package tcss543.auction;

import java.util.*;
import tcss543.*;

public class AuctionGraphBuilder {
	public AuctionGraph buildAuctionGraph(int[][] matching, DeviceGraph graph) {
		LinkedList<Requestor> requestors = new LinkedList<Requestor>();
		LinkedList<HelperGroup> helperGroups = new LinkedList<HelperGroup>();
		
		for (int i = 0; i < matching.length; i++) {
			if (matching[i] != null) {
				Requestor requestor = new Requestor();
				requestor.i = i;
				requestor.bidPrice = graph.getB()[i];
				
				HelperGroup helperGroup = new HelperGroup();
				helperGroup.helperIndices = matching[i];
				helperGroup.helperCount = matching[i].length;
				double maxAskPrice = 0;
				for (int j = 0; j < matching[i].length; j++) {
					double askPrice = graph.getA()[matching[i][j]];
					if (askPrice > maxAskPrice) {
						maxAskPrice = askPrice;
					}
				}
				
				helperGroup.askPrice = helperGroup.helperCount * maxAskPrice;
				requestor.helperGroup = helperGroup;
				
				requestors.add(requestor);
				helperGroups.add(helperGroup);
			}
		}
		
		Collections.sort(requestors, new RequestorComparator());
		Collections.sort(helperGroups, new HelperGroupComparator());
		
		AuctionGraph auctionGraph = new AuctionGraph();
		auctionGraph.requestors = requestors;
		auctionGraph.helperGroups = helperGroups;
		return auctionGraph;
	}
}
